﻿/*Write a program that finds the maximal sequence of equal elements in an array.
		Example: {2, 1, 1, 2, 3, 3, 2, 2, 2, 1} - {2, 2, 2}.*/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ex4_Sequel_of_equal_elements
{
    class Equal_Sequence
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Write a program that finds the maximal sequence of equal elements in an array.");
		    Console.WriteLine("Example: {2, 1, 1, 2, 3, 3, 2, 2, 2, 1}  {2, 2, 2}.\n\n");
            Console.WriteLine("Enter array length!");
            byte length = byte.Parse(Console.ReadLine());
            Console.WriteLine();
            //Initialising 2 arrays. The first one will be examined and the result will be given to the second one.
            int[] arrOne = new int[length];
            int[] count = new int[length];
            string[] sequence = new string[length];
            //Position in the second array.
            int number = 0;
            //Give value to the elements of the examined array.
            for (int i = 0; i < length; i++)
            {
                arrOne[i] = int.Parse(Console.ReadLine());
            }
            Console.WriteLine();
            //Check if the corresponding element is equal to the previous one. Then store the number of equal elements
            //in a position in the second array. After the sequence finishes moves the position in the second array and
            //does the same for the next sequence.
            for (int i = 1; i < length; i++)
            {
                if (arrOne[i] == arrOne[i - 1])
                {
                    //Stores the sequence of numbers in a string.
                    sequence[number] += arrOne[i].ToString() + ",";
                    count[number] += 1;
                }
                else if(arrOne[i] != arrOne[i - 1])
                {
                    number++;
                }
            }
            //Writes the maximum number of equal numbers
            Console.WriteLine("-------------------------------------------------");
            Console.WriteLine("Result:\n");
            Console.WriteLine("Length of the max sequence of equal elements:\n");
            Console.WriteLine(count.Max() + 1 +"\n");
            int index = Array.IndexOf(count, count.Max());
            char[] ofString = sequence[index].ToCharArray();
            Console.WriteLine("Sequence:\n");
            Console.WriteLine(sequence[index] + new string(ofString[0], 1));
            Console.WriteLine("-------------------------------------------------");
        }
    }
}
